<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/13
 * Time: 10:29
 */

namespace App\Rep;


use App\Models\Material;
use App\Models\MaterialDrawLog;
use App\Models\MaterialRetreatLog;
use App\Models\ProductMaterial;
use App\Models\WarehouseOrderChild;
use Illuminate\Support\Facades\DB;
use Lcobucci\JWT\Exception;

class MaterialRep
{

    /**
     * 获取材料列表
     * @param $params
     */
    public function getMaterialListRep($params){
        $list = Material::Search($params)
            ->with(['supplier','type','product'=>function($q){
                $q->select('product.id','product.product_name');
            },'warehouse'])
            ->orderBy('id','desc')
            ->paginate($params['per_page']??100);
        $lists = $list->toArray();
        //p($lists['data']);
        foreach($lists['data'] as $k=>$v){
            $materialStock = $this->getMaterialStockRep($v['id']);
            $lists['data'][$k]['materialStock'] = $materialStock;
        }
        return (object)$lists;

    }

    /**
     * 添加材料
     * @param $params
     */
    public function createMaterialRep($params){
        DB::beginTransaction();
        try {
            if(isset($params['id']) && !empty($params['id'])){
                $model = Material::where('id',$params['id'])->first();
            }else{
                $model = new Material();
            }
            $model->name = $params['name'];
            $model->code = $params['code'];
            $model->type_id = $params['type_id'];
            $model->model_number = $params['model_number']??'';
            $model->specification = $params['specification']??'';
            $model->color = $params['color'];
            $model->w_price = $params['w_price'];
            $model->unit = $params['unit'];
            $model->supplier_id = $params['supplier_id'];//供应商
            //$model->initial_inventory = $params['initial_inventory'];
            $model->initial_min = $params['initial_min'];
            $model->initial_max = $params['initial_max'];
            $model->warehouse_id = $params['warehouse_id'];

            $model->save();
            foreach($params['product_ids'] as $v){
                ProductMaterial::updateOrCreate(['product_id'=>$v,'material_id'=>$model->id],
                    ['product_id'=>$v,'material_id'=>$model->id]);
            }
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>'success'];
        }catch (Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$exception->getMessage()];
        }

    }

    /**
     * 删除材料
     * @param $params
     */
    public function delMaterialRep($params){
        $model = Material::where('id',$params['id'])->first();
        $model->is_del = $params['is_del'];
        $model->save();
        return ['code'=>0,'data'=>$model,'msg'=>'success'];
    }

    /**
     * 获取材料库存
     * @param $params
     * @return void
     */
    public function getMaterialStockRep($material_id){
        //入库
        $sum = WarehouseOrderChild::where('material_id',$material_id)->sum('num');
        //领料
        $draw_sum = MaterialDrawLog::where('material_id',$material_id)->sum('num');
        //退料
        $retreat_sum = MaterialRetreatLog::where('material_id',$material_id)->sum('num');
        return $sum + $retreat_sum - $draw_sum;
    }

}
